package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relations;

/**
 * Servlet implementation class Entity
 */
public class RelationAddTrashFromFavorite extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RelationAddTrashFromFavorite() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			String ds_type = (String) regtJSON.get("ds_type");
			int ds_id = Integer.parseInt((String) regtJSON.get("ds_id"));
			String user = (String) regtJSON.get("user");
			Relations.addTrash(ds_type, ds_id, user, id);
			Relations.delFavorite(ds_type, ds_id, user, id);
			JSONObject result = new JSONObject();
			result.put("result", "ok");
			response.getWriter().print(result);
		}
	}
}
